from machine import *
from smartcar import *
from seekfree import *
from display import *
from seekfree import IMU963RX
from seekfree import WIRELESS_UART
from seekfree import BLDC_CONTROLLER
from seekfree import MOTOR_CONTROLLER
from seekfree import TSL1401

from global_var import *
import gc
import time
import os
import io
import math

Direct_Left =  Pin('B14', Pin.IN, pull=Pin.PULL_UP_47K, value = True)
Direct_Right =  Pin('D22', Pin.IN, pull=Pin.PULL_UP_47K, value = True)
Direct_Down =  Pin('D23', Pin.IN, pull=Pin.PULL_UP_47K, value = True)
Direct_Up =  Pin('B15', Pin.IN, pull=Pin.PULL_UP_47K, value = True)
Direct_Center =  Pin('D20', Pin.IN, pull=Pin.PULL_UP_47K, value = True)
Button1 =  Pin('D21', Pin.IN, pull=Pin.PULL_UP_47K, value = True)
imu = IMU963RX()
encoder_l = encoder("C1", "C0", True)
encoder_r = encoder("C3", "C2")
wireless = WIRELESS_UART(115200)
motor_l = MOTOR_CONTROLLER(MOTOR_CONTROLLER.PWM_D4_DIR_D5, 17000, duty = 0, invert = True)
motor_r = MOTOR_CONTROLLER(MOTOR_CONTROLLER.PWM_D6_DIR_D7, 17000, duty = 0, invert = True)
cs = Pin('B29' , Pin.OUT, pull=Pin.PULL_UP_47K, value=1)
cs.high()
cs.low()
rst = Pin('B31' , Pin.OUT, pull=Pin.PULL_UP_47K, value=1)
dc  = Pin('B5' , Pin.OUT, pull=Pin.PULL_UP_47K, value=1)
blk = Pin('C21' , Pin.OUT, pull=Pin.PULL_UP_47K, value=1)
drv = LCD_Drv(SPI_INDEX=2, BAUDRATE=60000000, DC_PIN=dc, RST_PIN=rst, LCD_TYPE=LCD_Drv.LCD200_TYPE)
lcd = LCD(drv)
lcd.color(0xFFFF, 0x0000)
lcd.mode(2)

time.sleep_ms(500)
lcd.clear(0x0000)
#
Param_lst = [0] * 100
try:
    user_file = io.open("user_data.txt", "r+")
except:
    print("user_data.txt 文件不存在，新建该文件.")
    user_file = io.open("user_data.txt", "w+")

user_file.seek(0)
for idx, param in enumerate(user_file):
    if param == '\n':
        continue
    Param_lst[idx] = float(param)
user_file.close()
